#include "MsgRounter.h"
#include "Game.h"

MsgRounter::MsgRounter()
{
	//ctor
}

MsgRounter::~MsgRounter()
{
	//dtor
}

void MsgRounter::ServerRounter(Message* _pMsg)
{
	switch(_pMsg->m_cmd){
	case::eCmd::C2G_Role_Info:
		{
			Debug::Log("case::eCmd::C2G_Role_Info:");
			bool isCreateRole = false;
			Message Msg(_pMsg->m_fd, eCmd::G2C_Role_Info);
			Msg.AddBool(isCreateRole);
			Message::Send(&Game::GetInstance().GetSock().GetMsgQunue(), &Msg);
		}
		break;
	case eCmd::SYS_Check_Answer:
		{
			int retKey = _pMsg->GetInt();
			bool isServer = _pMsg->GetBool();

			// 返回key不对， 不是我们程序 关闭链接
			if(retKey != 8259 || isServer)
			{
				Debug::Warn("Not our process, shutdown connect.");
				Game::GetInstance().GetSock().ShutDownAConnect(_pMsg->m_fd);
				break;
			}

			Client* pClient = Game::GetInstance().GetSock().GetClient(_pMsg->m_fd);
			if(pClient == nullptr)
			{
				Debug::Error("Not found Client in socket");
				return;
			}

			// 链接的socket客户端通过验证
			pClient->m_isPassCheck = true;
			{// 是客户端程序
				// YU_TODO: 创建player
				Debug::Log("Create Player, please.");
			}

		}
		break;
		default:;
	}

}

void MsgRounter::ClientRounter(Message* _pMsg)
{
	switch(_pMsg->m_cmd){
	case eCmd::SYS_Check_Ask:
		{
			//Debug::Log("case eCmd::SYS_Check_Ask:");
			int key = _pMsg->GetInt();
			if(key != 9527)
			{
				Debug::Error("key Error ???");
				break;
			}

			int retKey = 8259;
			bool isServer = true;

			Message Msg(Game::GetInstance().GetSockClient().GetFd(), eCmd::SYS_Check_Answer);
			Msg.AddInt(retKey);
			Msg.AddBool(isServer);
			Message::Send(&Game::GetInstance().GetSockClient().GetMsgQunue(), &Msg);
		}
		break;
	case eCmd::W2G_Login_Info:
		{
			//Debug::Log("eCmd::W2G_Login_Info");
			Login LoginData = Game::GetInstance().GetLogin();
			int id = LoginData.m_GameServerInfo.m_id;
			string serverName = LoginData.m_GameServerInfo.m_name;
			int clientCount = LoginData.m_GameServerInfo.m_count;
			string loginIp = LoginData.m_GameServerInfo.m_ip;
			int loginPort =  LoginData.m_GameServerInfo.m_port;

			Message Msg(Game::GetInstance().GetSockClient().GetFd(), eCmd::G2W_Login_Info);
			Msg.AddInt(id);
			Msg.AddString(serverName);
			Msg.AddInt(clientCount);
			Msg.AddString(loginIp);
			Msg.AddInt(loginPort);
			Message::Send(&Game::GetInstance().GetSockClient().GetMsgQunue(), &Msg);
		}
		break;
		default:;
	}
}










//// 发送消息路由入口
//void MsgRounter::SendRounter(Message* _pMsg)
//{
//	_pMsg->Encode();
//
//	if(!MsgCheck(_pMsg)){
//		return;
//	}
//
//
//	// YU_TODO: 应该是不同的 write at here。 各种if吧
//	// m_ServerSock.GetMsgQunue().PushSendMsg(_pMsg);
//}
//
//
//// 消息路由
//bool MsgRounter::MsgCheck(Message* _pMsg)
//{
//	int cmd = (int)_pMsg->m_cmd;
//	if(( cmd < MSG_CMD_MIN) && (cmd > MSG_CMD_MAX))
//	{
//		cout<< "Error. cmd beyond limit."<< endl;
//		cout<< "Error at Msg.Send(), sorry, I don't know which shall I send to."<< endl;
//		return false;
//	}
//
//	return true;
//}
